import 'dart:math';

import 'package:flutter/material.dart';
import 'package:mobile_app.core/core/domain/entities/dialect.dart';
import 'package:mobile_app.core/core/domain/entities/learning_content/lexical_item/lexical_item.dart';
import 'package:mobile_app.core/core/presentation/widget_config.dart';
import 'package:mobile_app.home.core/core/presentation/localization/translation.dart';
import 'package:mobile_app.home.core/core/presentation/theme/home_core_theme.dart';
import 'package:mobile_app.home.core/core/presentation/widgets/pronunciation_caution_button.dart';
import 'package:mobile_app.home.core/core/presentation/widgets/scalable_lexical_item_text.dart';

mixin CardViewMixin {
  String getLocalizedLexicalItemUnit({
    required LexicalItem lexicalItem,
    required BuildContext context,
  }) {
    if (lexicalItem is InbuiltAdjective) {
      return HomeCoreTranslation.of(context).cardLexicalItemUnitAdjective;
    }

    if (lexicalItem is InbuiltAdverb) {
      return HomeCoreTranslation.of(context).cardLexicalItemUnitAdverb;
    }

    if (lexicalItem is InbuiltConjunction) {
      return HomeCoreTranslation.of(context).cardLexicalItemUnitConjunction;
    }

    if (lexicalItem is InbuiltNoun) {
      return HomeCoreTranslation.of(context).cardLexicalItemUnitNoun;
    }

    if (lexicalItem is InbuiltNumeral) {
      return HomeCoreTranslation.of(context).cardLexicalItemUnitNumeral;
    }

    if (lexicalItem is InbuiltPreposition) {
      return HomeCoreTranslation.of(context).cardLexicalItemUnitPreposition;
    }

    if (lexicalItem is InbuiltPronoun) {
      return HomeCoreTranslation.of(context).cardLexicalItemUnitPronoun;
    }

    if (lexicalItem is InbuiltVerb) {
      return HomeCoreTranslation.of(context).cardLexicalItemUnitVerb;
    }

    if (lexicalItem is InbuiltPhrasalVerb) {
      return HomeCoreTranslation.of(context).cardLexicalItemUnitPhrasalVerb;
    }

    return '';
  }

  String? getLexicalItemTranscription({
    required LexicalItem lexicalItem,
    required Dialect dialect,
  }) {
    if (lexicalItem is InbuiltLexicalItem) {
      return '/\u200d${lexicalItem.transcriptions[dialect]}\u200d/';
    }
    if (lexicalItem is CustomLexicalItem) {
      return lexicalItem.transcription;
    }

    throw Exception();
  }

  int getLexicalItemNameMaxLines({
    required double textHeight,
    required double maxHeight,
  }) {
    if (textHeight < maxHeight * 0.35) {
      return 2;
    } else {
      return 1;
    }
  }

  Widget buildTranscriptionRow({
    required String lexicalItemTranscription,
    required double scaledLexicalItemNameFontSize,
    required bool safeToPronounce,
    required VoidCallback onPronunciationCautionButtonPressed,
    required BuildContext context,
  }) {
    final scaledTranscriptionFontSize = max(
      scaledLexicalItemNameFontSize * kPreferableLexicalItemTranscriptionRatio,
      HomeCoreTheme.of(context).textTheme.cardLexicalItemTranscription.fontSize! /
          kMaxLexicalItemTranscriptionShrinkFactor,
    );
    final scaledTranscriptionTextStyle =
        HomeCoreTheme.of(context).textTheme.cardLexicalItemTranscription.copyWith(
              fontSize: scaledTranscriptionFontSize,
            );

    final transcriptionWidget = ScalableLexicalItemText(
      text: lexicalItemTranscription,
      style: scaledTranscriptionTextStyle,
      maxLines: 1,
    );

    if (safeToPronounce) {
      return transcriptionWidget;
    }

    final pronunciationCautionButtonIconSize = scaledTranscriptionTextStyle.fontSize!;

    final pronunciationCautionButtonHorizontalPadding = pronunciationCautionButtonIconSize * 0.3;

    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      mainAxisSize: MainAxisSize.max,
      children: [
        SizedBox(
          width: pronunciationCautionButtonIconSize + pronunciationCautionButtonHorizontalPadding,
        ),
        transcriptionWidget,
        PronunciationCautionButton(
          size: pronunciationCautionButtonIconSize,
          padding: EdgeInsets.symmetric(
            horizontal: pronunciationCautionButtonHorizontalPadding,
          ),
          onPressed: onPronunciationCautionButtonPressed,
        ),
      ],
    );
  }
}
